package com.kuanter.kuanterauto.adapter;

import java.io.InputStream;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.cache.ObjectCache;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.entity.CardInfo;
import com.kuanter.kuanterauto.utils.ImageLoader;

public class CardAndNumberImageAdapter extends BaseAdapter {
	private List<CardInfo> cardInfos ;  
	private Activity activity;
	private ImageView card_iv;
	private TextView card_number_tv;
	private View view;
	private int index;
	private ProApplication proApp;
	private TextView guashi_tv;
	public InputStream is;
	
	
	public CardAndNumberImageAdapter(List<CardInfo> cardInfos, Activity activity) {
		this.cardInfos = cardInfos;
		this.activity = activity;
		proApp = (ProApplication)activity.getApplication();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cardInfos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return cardInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		index = position;
		view = null;
		if(null == convertView){
			view = View.inflate(activity, R.layout.card_and_number_image_item, null);
		}else{
			view = convertView;
		}
		this.initView();
		this.setContent();
		return view; 
	}
	private void setContent() {
		ImageLoader loder = new ImageLoader(activity);
		loder.DisplayImage(HttpConfigAPI.SERVER_URL+cardInfos.get(index).getCardPicPath(), card_iv, false);
		card_number_tv.setText(cardInfos.get(index).getCardNumber());
		if(null != cardInfos.get(index).getState()&&!"".equals(cardInfos.get(index).getState())){
			switch (Integer.valueOf(cardInfos.get(index).getState())) {
			//1未激活；2-已激活；3-已挂失；4-已损毁
			case 1:
				guashi_tv.setText("未激活");
				guashi_tv.setVisibility(View.GONE);
				break;
			case 2:
				guashi_tv.setText("已激活");
				guashi_tv.setVisibility(View.GONE);
				break;
			case 3:
				guashi_tv.setText("(已挂失)");
				guashi_tv.setTextColor(activity.getResources().getColor(R.color.red));
				guashi_tv.setVisibility(View.VISIBLE);
				break;
			case 4:
				guashi_tv.setText("已损毁");
				guashi_tv.setVisibility(View.VISIBLE);
				break;
			}
		}else{
			guashi_tv.setVisibility(View.GONE);
		}
	}

	private void initView() {
		card_iv = (ImageView)view.findViewById(R.id.card_iv);
		card_number_tv = (TextView)view.findViewById(R.id.card_number_tv);
		guashi_tv = (TextView)view.findViewById(R.id.guashi_tv);
		guashi_tv.setTextSize(16);
	}
	
	/*private class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
		private ImageView iv_card_image;
		private String imgPath;

		public BitmapWorkerTask(ImageView iv_card_image) {
			BitmapWorkerTask.this.iv_card_image = iv_card_image;
		}

		// Decode image in background.
		@Override
		protected Bitmap doInBackground(String... params) {
			try {
				imgPath = params[0];
				is = getStreamFromURL(params[0]);
			} catch (Exception e) {
				Log.i("getStreamFromURL", "getStreamFromURL is error");
			}
			Bitmap card_image_bitmap = BitmapFactory.decodeStream(is);
			return card_image_bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			iv_card_image.setImageBitmap(bitmap);
			//图片存在内存
			ObjectCache.set(imgPath, BitmapFactory.decodeStream(is));
		}
	}*/

	public InputStream getStreamFromURL(String imageURL) throws Exception {
		Log.i("wuyang", "get bitmap --------------net");
		InputStream is = null;
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(imageURL);
		HttpResponse response = client.execute(get);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		}
		return is;
	}
	
	/*public void loadAndSetBitmap(String imageUrl, ImageView imageView) {    
		BitmapWorkerTask task = new BitmapWorkerTask(imageView);     
		task.execute(imageUrl);
	}*/

}
